package com.unmarshaller.propertiesunmarshaller;

import java.io.File;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import javax.xml.crypto.dsig.TransformException;

import com.unmarshaller.propertiesunmarshaller.transformer.BooleanTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.ByteTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.CharTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.ClassTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.DoubleTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.EnumTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.FileTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.FloatTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.InetSocketAddressTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.IntegerTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.LongTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.PatternTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.ShortTransformer;
import com.unmarshaller.propertiesunmarshaller.transformer.StringTransformer;

/**
 * 类型工厂
 * @author hongxiao.z
 * @date	2013-10-21
 */
public class PropertyTransformerFactory 
{
	private static Map<Class<?>, ITransformer> map = new HashMap<Class<?>, ITransformer>();
	
	public static ITransformer getTransformer(Class<?> clazz) throws TransformException
	{
		ITransformer itrans = map.get(clazz);
		
		if(itrans == null)
		{
			throw new TransformException("找不到支持的类型转换器。");
		}
		
		return itrans;
	}
	
	private static void register(Class<?> key, ITransformer value)
	{
		map.put(key, value);
	}
	
	static
	{
		register(	Byte.class		, 	new ByteTransformer()	);
		register(	Short.class		, 	new ShortTransformer()	);
		register(	Integer.class	, 	new IntegerTransformer());
		register(	Long.class		, 	new LongTransformer()	);
		register(	Float.class		, 	new FloatTransformer()	);
		register(	Double.class	, 	new DoubleTransformer()	);
		register(	String.class	, 	new StringTransformer()	);
		register(	Character.class	, 	new CharTransformer()	);
		register(	Pattern.class	, 	new PatternTransformer());
		register(	File.class		, 	new FileTransformer()	);
		register(	Boolean.class	, 	new BooleanTransformer());
		register(	Enum.class		, 	new EnumTransformer()	);
		register(	Class.class		, 	new ClassTransformer()	);
		
		register(	InetSocketAddress.class, new InetSocketAddressTransformer());
		
		register(	Byte.TYPE		, 	new ByteTransformer()	);
		register(	Short.TYPE		, 	new ShortTransformer()	);
		register(	Integer.TYPE	, 	new IntegerTransformer());
		register(	Long.TYPE		, 	new LongTransformer()	);
		register(	Float.TYPE		, 	new FloatTransformer()	);
		register(	Double.TYPE		, 	new DoubleTransformer()	);
		register(	Character.TYPE	, 	new CharTransformer()	);
		register(	Boolean.TYPE	, 	new BooleanTransformer());
	}
}
